<template>
  <div class="page-wrap-login">
    <div class="login-modal">
      <div class="title-container">
        <h3>登录到</h3>
        <h3>GIMI SHARE</h3>
      </div>
      <el-form
        ref="formRef"
        :model="loginForm"
        label-width="100px"
        class="login-ruleForm"
        label-position="top"
      >
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            type="text"
            placeholder="请输入登录账号"
            :prefix-icon="User"
            size="large"
          />
        </el-form-item>
        <el-form-item prop="password" style="margin-bottom: 6px;">
          <el-input
            show-password
            v-model="loginForm.password"
            type="password"
            placeholder="请输入登录密码"
            :prefix-icon="Lock"
            size="large"
            @keydown.enter="handleLogin"
          />
        </el-form-item>
        <el-form-item style="text-align: right;">
          <el-checkbox v-model="rememberAccount" label="记住账号" size="large" />
        </el-form-item>
        <el-form-item>
          <el-button
            @click="handleLogin" type="primary" size="large">立即登录</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script setup lang="ts" name="Login">
import { ref, reactive } from 'vue'
import { apiUserLogin } from '@/http/service'
import { User, Lock } from '@element-plus/icons-vue'
import type { User as UserType } from '@/http/type'
import { useUsersStore } from '@/stores'
import { storeToRefs } from 'pinia'

const { userInfo, permissionCodes } = storeToRefs(useUsersStore())
interface LoginUser extends UserType {
  permissionCodes: Array<string>
  accessToken: string
}
const rememberAccount = ref(false)
const loginForm = reactive({
  username: '',
  password: ''
})

async function handleLogin () {
  if (!loginForm.username || !loginForm.password) return
  const data = await apiUserLogin<LoginUser>(loginForm)
  localStorage.setItem('permission', JSON.stringify(data.permissionCodes))
  localStorage.setItem('token', data.accessToken)
  localStorage.setItem('userInfo', JSON.stringify({
    ...data,
    permissionCodes: undefined,
    accessToken: undefined
  }))
  permissionCodes.value = data.permissionCodes
  userInfo.value = data
  location.href = location.origin + import.meta.env.VITE_APP_BASE_URL
}
</script>

<style lang="scss" scoped>
.page-wrap-login {
  position: fixed;
  inset: 0;
  background-image: url('@/assets/images/login_bg.webp');
  background-size: cover;
  background-position: 100%;
  .login-modal {
    min-width: 360px;
    min-height: 480px;
    position: absolute;
    top: 22%;
    left: 5%;
    .title-container {
      h3 {
        font-size: 36px;
        font-weight: 600;
        color: rgba(0,0,0,.9);
        font-family: PingFang SC, Microsoft YaHei, Arial Regular;
        line-height: 44px;
        &:last-child {
          margin-top: 4px;
        }
      }
    }
    .login-ruleForm {
      margin-top: 40px;
      .el-form-item {
        margin-bottom: 20px;
        button {
          width: 100%;
          margin-top: 20px;
          outline: none;
          border: none;
          background-color: #0052d9;
          transition: .2s;
          &:active, &:focus {
            background-color: #0052d9;
          }
          &:hover {
            opacity: .8;
          }
        }
        .youke-login {
          color: #0052d9;
          cursor: pointer;
          transition: .3s;
          user-select: none;
          &:active {
            opacity: .6;
          }
        }
        :deep(.el-checkbox) {
          .el-checkbox__input {
            .el-checkbox__inner {
              border-color: rgba(0,0,0,0.6);
              width: 15px;
              height: 15px;
            }
            &.is-checked {
              .el-checkbox__inner {
                background-color: #0052d9;
                border-color: #0052d9;
                &::after {
                  border-color: #fff;
                  border-width: 2px;
                }
              }
            }
          }
          .el-checkbox__label {
            font-size: 13px;
            padding-left: 6px;
            position: relative;
            top: -1px;
            color: rgba(0,0,0,0.6);
          }
          &.is-checked {
            .el-checkbox__label {
              color: #0052d9;
            }
          }
        }
      }
      :deep(.el-input__wrapper) {
        border-radius: 2px;
        box-shadow: none;
        border: 1px solid #e8e8e8;
        .el-icon {
          color: #ccc;
          font-size: 16px;
        }
        .el-input__inner {
          line-height: 32px;
        }
      }
    }
  }
}
</style>
